package dao;

import model.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDateBase {
    private String dbdriver="com.mysql.jdbc.Driver";
    private String dburl="jdbc:mysql://localhost:3306/sctu-javaweb-2018?serverTimezone=UTC&characterEncoding=UTF-8";
    private String dbuser="root";
    private String dbpassword="123456";

    public List<User> getuserList(String username)throws Exception{
        List<User> userList=new ArrayList<User>();
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        Class.forName(dbdriver);
        connection= DriverManager.getConnection(dburl,dbuser,dbpassword);

        String sql="select * from user_information WHERE username=?";
        preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        resultSet=preparedStatement.executeQuery();

        while (resultSet.next()){
            User user=new User();
            user.setId(resultSet.getInt(1));
            user.setUsername(resultSet.getString(2));
            user.setPassword(resultSet.getString(3));
            user.setHeadImg(resultSet.getString(4));
            user.setSign(resultSet.getString(5));
            user.setNickname(resultSet.getString(6));
            user.setSex(resultSet.getString(7));
            user.setOccupation(resultSet.getString(8));
            user.setLocal(resultSet.getString(9));
            user.setEmail(resultSet.getString(10));
            user.setPersonalIntroduction(resultSet.getString(11));
            user.setBalance(resultSet.getFloat(12));
            user.setRedPaper(resultSet.getFloat(13));
            user.setCoupon(resultSet.getFloat(14));
            user.setCredit(resultSet.getString(15));
            userList.add(user);
        }

        return userList;
    }
}
